// pages/main/Myself.js
import api from '../../utils/api.js';
import network from '../../utils/network.js';
import notification from '../../utils/notification_center.js'
let app = getApp();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    userInfo: null,
    shouNum:0,
    faNum:0,
    gongCheNum:0,
    bangongNum:0,
    qiyeNum:0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    // let that = this;
    // wx.getStorage({
    //   key: 'jinxin_user_info',
    //   success(res) {
    //     if (res.data) {
    //       let userInfo = res.data
    //       app.globalData.userInfo = userInfo
    //       that.setData({
    //         userInfo: userInfo ? userInfo : null
    //       })
    //     }
    //   }
    // })
    this.refreshUserInfo();
    notification.addNotification("Myself", this.refreshUserInfo, this);
  },
  onShow:function(){
    if (app.globalData.userInfo) {
      network.postJson(this, api.getStaticNum, {
        adminid: app.globalData.userInfo.uid,
        token: app.globalData.userInfo.token,

      }).then(res => {
        let result = res.data.data;
        if(res.data.code == 199){
          wx.redirectTo({
            url: '/myself/login/Login'
          })
        }
        if (result) {
          if(result.allNum > 0){
            wx.setStorageSync('dbAllNum', result.allNum)
            wx.setTabBarBadge({
              index: 3,
              text: result.allNum.toString()
            })
            if(result.shouNum > 0){
              this.setData({
                shouNum:result.shouNum
              })
            }else{
              this.setData({
                shouNum:0
              })
            }
            if(result.faNum > 0){
              this.setData({
                faNum:result.faNum
              })
            }else{
              this.setData({
                faNum:0
              })
            }
            if(result.gongCheNum > 0){
              this.setData({
                gongCheNum:result.gongCheNum
              })
            }else{
              this.setData({
                gongCheNum:0
              })
            }
            if(result.bangongNum > 0){
              this.setData({
                bangongNum:result.bangongNum
              })
            }else{
              this.setData({
                bangongNum:0
              })
            }
            if(result.qiyeNum > 0){
              this.setData({
                qiyeNum:result.qiyeNum
              })
            }else{
              this.setData({
                qiyeNum:0
              })
            }
          }else{
             //移除角标
            wx.removeTabBarBadge({
              index: 3,
            })
            wx.setStorageSync('dbAllNum', 0)
            this.setData({
              shouNum:0,
              faNum:0,
              gongCheNum:0,
              bangongNum:0,
              qiyeNum:0
            })

          }     
        } else {
          //移除角标
          wx.removeTabBarBadge({
            index: 3,
          })
        }
      })
        
    } else {
      //移除角标
      wx.removeTabBarBadge({
        index: 3,
      })
    }
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {
    notification.removeNotification("Myself", this);
  },

  goLogin() {
    wx.navigateTo({
      url: '/myself/login/Login'
    })
  },

  goAccounts() {
    wx.navigateTo({
      url: '/myself/login/Accounts'
    })
  },

  goFileRegister(e) {
    if (app.globalData.userInfo) {
      wx.navigateTo({
        url: '/myself/file/FileRegister?title=' + e.currentTarget.dataset.title
      })
    } else {
      wx.navigateTo({
        url: '/myself/login/Login'
      })
    }
  },
  goFilePost(e) {
    if (app.globalData.userInfo) {
      wx.navigateTo({
        url: '/myself/file/FilePost?title=' + e.currentTarget.dataset.title
      })
    } else {
      wx.navigateTo({
        url: '/myself/login/Login'
      })
    }
  },
  goQiyeFilePost(e) {
    if (app.globalData.userInfo) {
      wx.navigateTo({
        url: '/myself/file/QiyeFilePost?title=' + e.currentTarget.dataset.title
      })
    } else {
      wx.navigateTo({
        url: '/myself/login/Login'
      })
    }
  },

  goSigningList() {
    if (app.globalData.userInfo) {
      wx.navigateTo({
        url: '/myself/file/SigningList'
      })
    } else {
      wx.navigateTo({
        url: '/myself/login/Login'
      })
    }
  },

  goIssueList() {
    if (app.globalData.userInfo) {
      wx.navigateTo({
        url: '/myself/file/IssueList'
      })
    } else {
      wx.navigateTo({
        url: '/myself/login/Login'
      })
    }
  },
  goQiyeIssueList() {
    if (app.globalData.userInfo) {
      wx.navigateTo({
        url: '/myself/file/QiyeIssueList'
      })
    } else {
      wx.navigateTo({
        url: '/myself/login/Login'
      })
    }
  },

  goBusApplyList() {
    if (app.globalData.userInfo) {
      wx.navigateTo({
        url: '/myself/apply/BusApplyList'
      })
    } else {
      wx.navigateTo({
        url: '/myself/login/Login'
      })
    }
  },

  goSuppliesApplyList() {
    if (app.globalData.userInfo) {
      wx.navigateTo({
        url: '/myself/apply/SuppliesApplyList'
      })
    } else {
      wx.navigateTo({
        url: '/myself/login/Login'
      })
    }
  },

  goHealthRecord() {
    if (app.globalData.userInfo) {
      wx.navigateTo({
        url: '/myself/daily/HealthRecord'
      })
    } else {
      wx.navigateTo({
        url: '/myself/login/Login'
      })
    }
  },

  goCollection() {
    wx.navigateTo({
      url: '/myself/collection/Collection'
    })
  },

  goForollow(){
    wx.navigateTo({
      url: '/myself/forollow/Forollow'
    })
  },

  goAbout() {
    wx.navigateTo({
      url: '/myself/about/About'
    })
  },

  goDailyWork(){
    if (app.globalData.userInfo) {
      wx.navigateTo({
        url: '/myself/daily/WorkList'
      })
    } else {
      wx.navigateTo({
        url: '/myself/login/Login'
      })
    }
  },

  refreshUserInfo() {
    if (app.globalData.userInfo) {
      console.log(app.globalData.userInfo)
      let userInfo = app.globalData.userInfo
      this.setData({
        userInfo: userInfo ? userInfo : null
      })
    }
  }
})